/**
 * Subclass van ui_AddRuleDialog
 */
#include "addruledialog.h"
#include <QDebug>
#include <QFileDialog>
#include <iostream.h>

/**
 * Constructor
 */
AddRuleDialog::AddRuleDialog(QDialog *parent) : QDialog(parent)
{
	setupUi(this);
	ruleID=0;
	//Set validators
	
	//Set filtertab buttons
	tabAddButton=new QToolButton(this);
	tabDelButton=new QToolButton(this);
	tabAddIcon=QIcon(":add");	
	tabDelIcon=QIcon(":delete");	
	tabAddButton->setIcon(tabAddIcon);
	tabDelButton->setIcon(tabDelIcon);
	filterTab->setCornerWidget(tabAddButton,Qt::TopLeftCorner);
	filterTab->setCornerWidget(tabDelButton,Qt::TopRightCorner);
	this->setWindowModality(Qt::ApplicationModal);

	connect(tabAddButton,SIGNAL(released()),this,SLOT(addFilterButtonPressed()));
	connect(tabDelButton,SIGNAL(released()),this,SLOT(delFilterButtonPressed()));
	
}


/**
 * Vult de velden van de dialog.
 */
void AddRuleDialog::fillForm(Value rule,int ruleID)
{
	int currentIndex=0;
	QString direction="";

	this->ruleID=ruleID;

	//Clean fields
	filterTab->clear();
	reserveLineEdit->setText("");	
	limitLineEdit->setText("");
	priorityLineEdit->setText("");
	

	if(rule.isMember("rate"))
		reserveLineEdit->setText(QString().fromStdString(rule["rate"].asString()));
	if(rule.isMember("limit"))
		limitLineEdit->setText(QString().fromStdString(rule["limit"].asString()));
	if(rule.isMember("priority"))
		priorityLineEdit->setText(QString().fromStdString(rule["priority"].asString()));

	if(rule.isMember("direction"))
	{
		direction=QString().fromStdString(rule["direction"].asString());
		for(int comboCount=0; comboCount< directionComboBox->count(); comboCount++)
		{
			if(directionComboBox->itemText(comboCount)==direction)
			{
				directionComboBox->setCurrentIndex(comboCount);
				break;
			}		
		}

	}
	if(!rule["filters"].empty())
	{
		for(int filterCount=1; filterCount<rule["filters"].size(); filterCount++)
		{
			
			//filterTab->insertTab(filterCount,filterWidget,"filter: "+QString().setNum(filterCount));
			addNewFilterTab(rule["filters"][filterCount]);
		}
		return;
	}

	//Make at least one filterWidget.
	//filterWidget=new FilterWidget(this);		

	addNewFilterTab(rule);
	/**	
	validateInput();
	*/
}
void AddRuleDialog::addNewFilterTab( Value filter)
{
	FilterWidget *filterWidget;
	filterWidget=new FilterWidget(this);		
	filterWidget->init(filter);
	filterTab->insertTab(filterTab->count()+1,filterWidget,"filter: "+QString().setNum(filterTab->count()+1));
}
void AddRuleDialog::addFilterButtonPressed()
{
	addNewFilterTab(Value(nullValue));
}
void AddRuleDialog::delFilterButtonPressed()
{
	if(filterTab->currentIndex()!=0)
		filterTab->removeTab(filterTab->currentIndex());
}

/**
 * Wanneer er op OK geclickt word, worden de gegevens eerst gevalideerd en dan naar de model getrapt.
 */
void AddRuleDialog::accept()
{
	Value rule=Value(objectValue);
	//Iterate over tabs..
	//qDebug() << filterTab->count();


	//Save input
	rule["rate"]=QString(((QLineEdit*)reserveLineEdit)->text()).toStdString();
	rule["limit"]=QString(((QLineEdit*)limitLineEdit)->text()).toStdString();
	rule["priority"]=QString(((QLineEdit*)priorityLineEdit)->text()).toStdString();
	rule["direction"]=QString(directionComboBox->currentText()).toStdString();
	
	for(int tabCount=0; tabCount<filterTab->count(); tabCount++)
		rule["filters"][tabCount+1]=((FilterWidget*)filterTab->widget(tabCount))->getFilter();


	emit saveRule(rule,ruleID);

	/**
	if(validateInput())
		return;
	Value settings=Value(objectValue);
	settings["monitor_user"]=usernameLineEdit->text().toStdString();
	//Wanneer store_password is afgevinkt sla het wachtwoord niet op.
	settings["monitor_passwd"]=passwordLineEdit->text().toStdString();
	settings["show_alert_duration"]=alertDialogDurationLineEdit->text().toInt();
	//Dit is voor de toekomst om een wav soundje achter een alert te kunnen configureren.
	settings["alert_cmd_path"]=execCmdLineEdit->text().toStdString();
	settings["store_password"]=storePasswordCheckBox->checkState();
	this->hide();
	emit newSettings(settings);
	*/
}


/**
  */
bool AddRuleDialog::validateInput()
{
	/**
	usernameLineEdit->setStyleSheet(NULL);
	passwordLineEdit->setStyleSheet(NULL);
	alertDialogDurationLineEdit->setStyleSheet(NULL);

	if(passwordLineEdit->text().length() < 1) {
		passwordLineEdit->setStyleSheet("background-color: yellow");
		passwordLineEdit->setFocus();
		giveFeedback(tr("Give password"));
		return true;
	}
	giveFeedback(NULL);
	return false;
	*/
}


/**
 * Geef de user feedback met deze functie in de settings dialog.
 */
void AddRuleDialog::giveFeedback(QString feedback)
{
	/**
	feedbackLabel->setText(feedback);
	feedbackLabel->adjustSize();
	feedbackLabel->setStyleSheet(NULL);
	if(feedback.size() > 1) {
		feedbackLabel->setStyleSheet("color: red; background-color: yellow");
	}
	*/
}

/**
 * Deze slot word aangeroepen wanneer de user cancel drukt.
 */
void AddRuleDialog::reject()
{
	
	emit rejectme();
}
